package io.renren.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sun.org.apache.regexp.internal.recompile;

import org.springframework.stereotype.Controller;

import io.renren.entity.DataAddmoneyEntity;
import io.renren.entity.DictDepartmentEntity;
import io.renren.service.DataAddmoneyService;
import io.renren.service.DictDepartmentService;
import io.renren.service.DictEmployeeService;
import io.renren.service.SysWorkLogService;
import io.renren.service.xtd.DataAddmoneyServiceXtd;
import io.renren.service.xtd.DataContractpayServiceXtd;
import io.renren.service.xtd.DictEmployeeServiceXtd;
import io.renren.service.xtd.HisDepartmentServiceXtd;
import io.renren.utils.AssignUtils;
import io.renren.utils.Common;
import io.renren.utils.ExcelUtils;
import io.renren.utils.PageUtils;
import io.renren.utils.R;
import io.renren.utils.ShiroUtils;


/**
 * 员工加款项表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-06-20 10:22:34
 */
@Controller
@RequestMapping("dataaddmoney")
public class DataAddmoneyController {
	@Autowired
	private DictEmployeeService dictEmployeeService;
	@Autowired
	private DictDepartmentService dictDepartmentService;
	@Autowired
	private DataContractpayServiceXtd dataContractpayServiceXtd;
	@Autowired
	private HisDepartmentServiceXtd hisDepartmentServiceXtd;
	@Autowired
	private DictEmployeeServiceXtd dictEmployeeServiceXtd;
    @Autowired
	private SysWorkLogService sysWorkLogService;
	@Autowired
	private DataAddmoneyService dataAddmoneyService;
	@Autowired
	private DataAddmoneyServiceXtd dataAddmoneyServiceXtd;
	
	@RequestMapping("/dataaddmoney.html")
	public String list(){
		return "dataaddmoney/dataaddmoney.html";
	}
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("dataaddmoney:list")
	public R list(Integer page, Integer limit,String value,String calTime){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		map.put("value", value);
		map.put("calTime", calTime);
		//查询列表数据
		List<DataAddmoneyEntity> dataAddmoneyList = dataAddmoneyService.queryList(map);
		int total = dataAddmoneyService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(dataAddmoneyList, total, limit, page);
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{id}")
	@RequiresPermissions("dataaddmoney:info")
	public R info(@PathVariable("id") String id){
		DataAddmoneyEntity dataAddmoney = dataAddmoneyService.queryObject(id);
		
		return R.ok().put("dataAddmoney", dataAddmoney);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("dataaddmoney:save")
	public R save(@RequestBody DataAddmoneyEntity dataAddmoney){
		dataAddmoneyService.save(dataAddmoney);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("dataaddmoney:update")
	public R update(@RequestBody DataAddmoneyEntity dataAddmoney){
		dataAddmoneyService.update(dataAddmoney);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("dataaddmoney:delete")
	public R delete(@RequestBody String[] ids){
		dataAddmoneyService.deleteBatch(ids);
		
		return R.ok();
	}
	
	/**
	 * 
	* @author Murphy Wu
	* 创建时间：2018年6月20日 上午11:36:19 
	* 说明 :
	 */
	@ResponseBody
	@RequestMapping("/upload")
	public R upload(@RequestParam("file") MultipartFile multfile,String time){
		try {
			if(time==null || time.length()==0 || time.indexOf("-")<0){
				Common.createWorkLog(dictEmployeeServiceXtd, sysWorkLogService,
	        			ShiroUtils.getUserEntity().getUsername(), "员工加款项导入失败",
	        			"未选择数据时间", "");
				return R.error("请选择数据时间");
			}
			R resultR = ExcelUtils.textAddMoneyExcel(multfile.getInputStream(),0);
			if (Integer.parseInt(resultR.get("code").toString())!=0) {
				Common.createWorkLog(dictEmployeeServiceXtd, sysWorkLogService,
	        			ShiroUtils.getUserEntity().getUsername(), "员工加款项导入失败",
	        			resultR.get("msg").toString(), time);
				return resultR;
				
			}
			Map<String, Object> mape=new HashMap<>();
			int total = dictDepartmentService.queryTotal(mape);
			if(total==0){
				Common.createWorkLog(dictEmployeeServiceXtd, sysWorkLogService,
	        			ShiroUtils.getUserEntity().getUsername(), "员工加款项导入失败",
	        			"未导入组织架构数据", time);
				return R.error("请先导入组织架构数据");
			}
			total = dictEmployeeService.queryTotal(mape);
			if(total==0){
				Common.createWorkLog(dictEmployeeServiceXtd, sysWorkLogService,
	        			ShiroUtils.getUserEntity().getUsername(), "员工加款项导入失败",
	        			"未导入员工数据", time);
				return R.error("请先导入员工数据");
			}
			total = dataContractpayServiceXtd.queryTotalXtd(mape);
			if(total==0){
				Common.createWorkLog(dictEmployeeServiceXtd, sysWorkLogService,
	        			ShiroUtils.getUserEntity().getUsername(), "员工加款项导入失败",
	        			"未导入合同数据", time);
				return R.error("请先导入合同数据");
			}
			Integer showType=0;
			Map<String, Object> map=new HashMap<>();
			map.put("year", Integer.parseInt(time.split("-")[0]));
			map.put("month", Integer.parseInt(time.split("-")[1]));
			total = hisDepartmentServiceXtd.queryTotalXtd(map);
			if (total>0) {
				Common.createWorkLog(dictEmployeeServiceXtd, sysWorkLogService,
	        			ShiroUtils.getUserEntity().getUsername(), "员工加款项导入失败",
	        			"已有历史数据，不可再导入", time);
				return R.error("已有历史数据，不可再导入");
			} else {
				if(Integer.valueOf(map.get("month").toString())==1){
					map.remove("year");
					map.put("year", Integer.parseInt(time.split("-")[0])-1);
					map.remove("month");
					map.put("month", 12);
				}else{
					map.remove("month");
					map.put("month", Integer.parseInt(time.split("-")[1])-1);
				}
				total = hisDepartmentServiceXtd.queryTotalXtd(map);
				Map<String, Object> hismap=new HashMap<>();
				int histotal = hisDepartmentServiceXtd.queryTotalXtd(hismap);
				if(total==0 && histotal!=0){
					Common.createWorkLog(dictEmployeeServiceXtd, sysWorkLogService,
		        			ShiroUtils.getUserEntity().getUsername(), "员工加款项导入失败",
		        			"请先将上个月组织架构转存历史，再导入", time);
					return R.error("请先将上个月组织架构转存历史，再导入");
				}
				Map<String, Object> mapb=new HashMap<>();
				mapb.put("year", time.split("-")[0]);
				mapb.put("month", time.split("-")[1]);
				total = dataContractpayServiceXtd.queryTotalXtd(mapb);
				if(total==0){
					showType=0;
				}else{
					showType=1;
				}
			}
			List<List<Object>> list = ExcelUtils.readExcel(multfile.getInputStream(),0);
			List<Map<String, Object>> dept = AssignUtils.assignToAddMoneyNewUuid(list,time.split("-")[0],time.split("-")[1]);
			Map<String, Object> mapb=new HashMap<>();
			mapb.put("year", time.split("-")[0]);
			mapb.put("month", time.split("-")[1]);
			dataAddmoneyServiceXtd.deleteXtd(mapb);
			dataAddmoneyServiceXtd.impExcelInList(dept);
            Common.createWorkLog(dictEmployeeServiceXtd, sysWorkLogService,
        			ShiroUtils.getUserEntity().getUsername(), "员工加款项导入成功",
        			"", time);
			return R.ok();
			
		} catch (Exception e) {
			Common.createWorkLog(dictEmployeeServiceXtd, sysWorkLogService,
        			ShiroUtils.getUserEntity().getUsername(), "员工加款项导入失败",
        			"导入失败", time);
			return R.error("导入失败");
		}
		
	}
}
